export default {
	state: {
		list: [
			{
				name:"强强",
				phone:"18312341234",
				path:"北京-北京市-丰台区",
				detailPath:"xxx街道",
				isdefault:false
			},
			{
				name:"强强",
				phone:"18312341234",
				path:"北京-北京市-丰台区",
				detailPath:"xxx街道",
				isdefault:false
			},
			{
				name:"强强",
				phone:"18312341234",
				path:"北京-北京市-丰台区",
				detailPath:"xxx街道",
				isdefault:false
			}
		],
		
	},
	
	getters: {
		//查找默认地址
		defaultPath: (state) => {
			return state.list.filter(v => {
				return v.isdefault
			})
		}
	},
	
	mutations: {
		//创建收货地址
		createPath(state, item) {
			state.list.unshift(item)
		},
		
		//删除收货地址
		delPath(state, index) {
			state.list.splice(index, 1)
		},
		
		//修改收货地址
		updatePath(state, {index, item}) {
			for (let key in item) {
				state.list[index][key] = item[key]
			}
		},
		
		//取消默认地址
		removeDefault(state) {
			state.list.forEach((v) => {
				if (v.isdefault) {
					v.isdefault = false
				}
			})
		}
	},
	
	actions: {
		//修改地址
		updatePathAction({commit}, obj) {
			if (obj.item.isdefault) {
				commit('removeDefault')
			}
			commit('updatePath', obj)
		},
		
		//增加地址
		addPathAction({commit}, item) {
			if (item.isdefault) {
				commit('removeDefault')
			}
			commit('createPath', item)
		}
	}
}